﻿using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace Morin.Wpf.Converters;

public class BrushToHexConverter : IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value is null) return null;

        string lowerHexString(int i)
        {
            return i.ToString("X2").ToLower();
        }

        var brush = (SolidColorBrush)value;
        var hex = lowerHexString(brush.Color.R) + lowerHexString(brush.Color.G) + lowerHexString(brush.Color.B);
        return "#" + hex;
    }

    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}